package com.mask.roll.edu.controller;


import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.util.RollResponse;
import com.mask.roll.edu.entity.Course;
import com.mask.roll.edu.entity.VO.course.CourseInfoVo;
import com.mask.roll.edu.entity.VO.course.CoursePublishVo;
import com.mask.roll.edu.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author hx
 * @since 2021-10-23
 */
@RestController
@RequestMapping("/edu/course")
@Api(tags = "课程管理")
public class CourseController {

    @Resource
    private CourseService courseService;

    /**
     * 添加课程
     * @param courseInfoVo
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加课程")
    public RollResponse add(@RequestBody CourseInfoVo courseInfoVo){

        String courseId = courseService.saveCourseInfo(courseInfoVo);
        return RollResponse.result(ServiceEnums.COURSE_ADD_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.COURSE_ADD_SUCCESS.getMessage(),courseId);
    }

    /**
     * 根据课程id获取课程信息
     * @param courseId
     * @return
     */
    @GetMapping("/get/{courseId}")
    public RollResponse getInfoByCourseId(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getInfoByCourseId(courseId);
        return RollResponse.result(ServiceEnums.COURSE_INFO_VO.getCode(),
                RollConstants.SUCCESS,ServiceEnums.COURSE_INFO_VO.getMessage(),courseInfoVo);
    }

    @PostMapping("/update")
    public RollResponse updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return RollResponse.result(ServiceEnums.COURSE_INFO_UPDATE.getCode(),
                RollConstants.SUCCESS,ServiceEnums.COURSE_INFO_UPDATE.getMessage());
    }


    /**
     * 根据课程id查询课程确认信息
     * @param id
     * @return
     */
    @GetMapping("/get/publish/course/info/{id}")
    @ApiOperation("根据课程id查询课程确认信息")
    public RollResponse getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo vo = courseService.publishCourseInfo(id);
        return RollResponse.result(ServiceEnums.COURSE_SELECT_PUBLISH_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.COURSE_SELECT_PUBLISH_SUCCESS.getMessage(),vo);
    }


    @PostMapping("/publish/{id}")
    @ApiOperation("课程发布")
    public RollResponse publishCourse(@PathVariable String id){
        Course course = new Course();
        course.setId(id);
        course.setStatus(RollConstants.NORMAL);
        courseService.updateById(course);
        return RollResponse.result(ServiceEnums.COURSE_PUBLISH_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.COURSE_PUBLISH_SUCCESS.getMessage());
    }

    @GetMapping("/get/list")
    @ApiOperation("获取课程列表")
    public RollResponse getCourseList(){
        List<Course> list = courseService.list(null);
        return RollResponse.result(ServiceEnums.COURSE_LIST_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.COURSE_LIST_SUCCESS.getMessage(),list);
    }

    //TODO 获取课程分页列表


    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除课程")
    public RollResponse deleteCourse(@PathVariable String id){
        courseService.removeCourse(id);
        return RollResponse.result(ServiceEnums.COURSE_DELETE_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.COURSE_DELETE_SUCCESS.getMessage());
    }
}

